/* 试卷参数模板 */
import React, { PureComponent } from 'react';
import { Table, Input, Button, Form, Modal, message } from 'antd';
import { history } from 'umi'
import { getTemplateList ,deleteTemplateList} from '@/services/paperInfo'
import Authorized from '@/components/Authorized/Authorized'
export default class Index extends PureComponent<any, any> {
    constructor(props: any) {
        super(props)
        this.state = {
            data: [],
            template_name:''
        }
    }
    applyList=()=>{
        const {template_name} = this.state;
        getTemplateList({template_name}).then(res => {
            this.setState({
                data: res
            })
        })
    }
    addHandle = () => {
        if(location.href.includes('tzExamInfo')){
            history.push('/addTzTemplate')
        }else{
            history.push('/addTemplate')
        }
    }
    goEdit=(id:any)=>()=>{
        if(location.href.includes('tzExamInfo')){
            history.push('/editTzTemplate/'+id)
        }else{
            history.push('/editTemplate/'+id)
        }
    }
    changeTemplateName=(e:any)=>{
        this.setState({
            template_name:e.target.value
        })
    }
    searchHandle=()=>{
        this.applyList();
    }
    componentDidMount() {
        this.applyList();
    }
    deleteHandle=(id:any)=>()=>{
        Modal.confirm({
            title:'提示',
            content:'确定删除该模板？',
            onOk:()=>{
                deleteTemplateList({id}).then(res=>{
                    if(res.status===1){
                        message.success('已删除')
                        this.applyList();
                    }else{
                        message.error('删除失败')
                    }
                })
            }
        })
    }
    render() {
        const { data } = this.state;
        const columns = [
            {
                title: '模板名称',
                dataIndex: 'template_name'
            },
            {
                title: '总分',
                dataIndex: 'total'
            },
            {
                title: '时长',
                dataIndex: 'test_time',
                render:(test_time:any)=>{
                    return test_time+'分钟'
                }
            },
            {
                title: '操作',
                dataIndex: 'id',
                render:(id:any)=>{
                    return <Authorized authority={[506,99999996]}>
                        <a onClick={this.goEdit(id)}>编辑</a>
                        <a style={{marginLeft:20}} onClick={this.deleteHandle(id)}>删除</a>
                    </Authorized>
                }
            },
        ]
        return (
            <div>
                <div style={{ overflow: 'hidden', marginBottom: 20 }}>
                    <Form layout="inline" style={{ float: 'right' }}>
                        <Form.Item><Input placeholder="输入模板名称" onChange={this.changeTemplateName}></Input></Form.Item>
                        <Form.Item><Button type="primary" onClick={this.searchHandle}>搜索</Button></Form.Item>
                        <Form.Item><Button type="primary" danger onClick={this.addHandle}>添加</Button></Form.Item>

                    </Form>
                </div>
                <Table rowKey="id" columns={columns} dataSource={data} />
            </div>
        )
    }
}

